package com.xuelongjiang.mqdemo.mq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.rabbitmq.client.Channel;
import com.xuelongjiang.domain.Orders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author xuelongjiang
 */
@Component
public class HelloReceiver {

    private static final Logger LOGGER = LoggerFactory.getLogger(HelloReceiver.class);

    @Value("${mq.queues.order.name}")
    private String orderQueue;



    @RabbitListener(queues = "hello")
    public void process(Message mes, Channel channel){

        String s1 = new String(mes.getBody());
        LOGGER.info("receiver:{}", s1);
        try {

            //channel.basicAck(mes.getMessageProperties().getDeliveryTag(),true);

            channel.basicNack(mes.getMessageProperties().getDeliveryTag(),false, false);

        } catch (Exception e) {
            LOGGER.error("exception",e);

        }
    }


    @RabbitListener(queues = "recharge.orders")
    public void ordersQueue(Message mes, Channel channel){

        String s1 = new String(mes.getBody());
        LOGGER.info(" order queue receiver:{}", s1);


        Orders orders = JSON.parseObject(s1, Orders.class);
        LOGGER.info(" order object queue receiver:{}", orders);
        try {

            //channel.basicAck(mes.getMessageProperties().getDeliveryTag(),true);

            channel.basicNack(mes.getMessageProperties().getDeliveryTag(),false, false);

        } catch (Exception e) {
            LOGGER.error("exception",e);

        }
    }



}

